package com.tony.business.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tony.business.util.TokenUtil;
import com.tony.domain.bo.JsonResult;
import com.tony.domain.bo.UserInfo;
import com.tony.domain.enumerate.TokenTypeEnum;
import com.tony.domain.exception.CustomException;
import com.tony.domain.dto.AdminUserDTO;
import com.tony.domain.dto.SelectAdminUserPageDTO;
import com.tony.domain.enumerate.DeleteEnum;
import com.tony.domain.enumerate.EnableEnum;
import com.tony.domain.vo.AdminUserPageVO;
import com.tony.domain.vo.AdminUserVO;
import com.tony.util.utils.PasswordUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tony.mapper.mapper.AdminUserMapper;
import com.tony.mapper.domain.po.AdminUser;

/**
 * @author tony
 * @date 2020/2/27 7:18 下午
 */
@Service
public class AdminUserService extends ServiceImpl<AdminUserMapper, AdminUser> {

    public JsonResult<AdminUserVO> add(AdminUserDTO dto, UserInfo userInfo) {
        if (existLoginName(dto.getLoginName(), null)) {
            throw new CustomException("登录名重复");
        }
        AdminUser user = new AdminUser();
        BeanUtils.copyProperties(dto, user);
        user.setCreateId(userInfo.getUserId());
        user.setCreateTime(LocalDateTime.now());
        user.setSalt(PasswordUtil.getSalt());
        user.setPassword(PasswordUtil.encode(user.getPassword(), user.getSalt()));
        user.setIsDel(DeleteEnum.UNDELETED);
        user.setIsEnable(EnableEnum.ENABLE);
        save(user);
        AdminUserVO vo = new AdminUserVO();
        BeanUtils.copyProperties(user, vo);
        return new JsonResult<>(vo);
    }

    public JsonResult<AdminUserVO> update(AdminUserDTO dto, UserInfo userInfo) {
        if (existLoginName(dto.getLoginName(), dto.getId())) {
            throw new CustomException("登录名重复");
        }
        AdminUser user = new AdminUser();
        BeanUtils.copyProperties(dto, user);
        user.setUpdateId(userInfo.getUserId());
        user.setUpdateTime(LocalDateTime.now());
        user.setSalt(PasswordUtil.getSalt());
        user.setPassword(PasswordUtil.encode(user.getPassword(), user.getSalt()));
        updateById(user);
        AdminUserVO vo = new AdminUserVO();
        BeanUtils.copyProperties(user, vo);
        return new JsonResult<>(vo);
    }

    public JsonResult<Page<AdminUserPageVO>> selectAdminUserPage(SelectAdminUserPageDTO pageDTO) {
        QueryWrapper<AdminUserPageVO> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(pageDTO.getName()), "u.name", pageDTO.getName());
        wrapper.like(StringUtils.isNotEmpty(pageDTO.getLoginName()), "u.login_name", pageDTO.getLoginName());
        wrapper.like(StringUtils.isNotEmpty(pageDTO.getPhone()), "u.phone", pageDTO.getPhone());
        wrapper.like(StringUtils.isNotEmpty(pageDTO.getRoleName()), "r.name", pageDTO.getRoleName());
        wrapper.eq("u.is_enable", pageDTO.getIsEnable().getValue());
        wrapper.eq("u.is_del", DeleteEnum.UNDELETED.getValue());
        wrapper.orderByDesc("u.create_time");
        Page<AdminUserPageVO> page = super.baseMapper.selectPageVO(new Page<>(pageDTO.getCurrent(), pageDTO.getSize()), wrapper);
        return new JsonResult<>(page);
    }

    private boolean existLoginName(String loginName, Long id) {
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.eq(AdminUser.COL_LOGIN_NAME, loginName);
        wrapper.notIn(id != null, AdminUser.COL_ID, id);
        return count(wrapper) > 0;
    }

    public JsonResult<Boolean> enable(Long id, EnableEnum enable, UserInfo userInfo) {
        AdminUser user = new AdminUser();
        user.setId(id);
        user.setIsEnable(enable);
        user.setUpdateId(userInfo.getUserId());
        user.setUpdateTime(LocalDateTime.now());
        return new JsonResult<>(updateById(user));
    }

    public UserInfo login(String name, String password) {
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.eq(AdminUser.COL_LOGIN_NAME, name);
        AdminUser user = getOne(wrapper);
        if (user == null) {
            throw new CustomException("用户为空");
        }
        if (EnableEnum.DISABLE.equals(user.getIsEnable())) {
            throw new CustomException("用户未启用");
        }
        if (!user.getPassword().equals(PasswordUtil.encode(password, user.getSalt()))) {
            throw new CustomException("密码错误");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setRole(user.getRoleId());
        userInfo.setType(TokenTypeEnum.ADMIN);
        return userInfo;
    }
}

